package com.anbinh.doctor.fragment.account;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.ServiceAdapter;
import com.anbinh.doctor.model.ResponseServices;
import com.anbinh.doctor.model.ResponseServices.Item;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentListService extends Fragment {
	private static FragmentListService fragment = null;
	private View mView;
	private CircleProgressBar loadding;
	private UserLogin userLogin;
	private ListView mGridView;
	private Item currentService;
	private List<Item> mList;

	public static FragmentListService newInstance() {
		if (fragment == null) {
			fragment = new FragmentListService();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);

		super.onAttach(activity);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_message, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mGridView = (ListView) mView.findViewById(R.id.listView1);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				currentService = mList.get(position);
				Bundle bundle = new Bundle();
				bundle.putInt(Utils.KEY, Utils.SERVICE_CHILD);
				bundle.putBoolean(Utils.CUSTOMBAR, true);
				bundle.putString(Utils.TITLE, mList.get(position).getLabel());
				Utils.getNewIntent(getActivity(), bundle);

			}
		});

		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);
		onLoadService();
	}

	public Item getCurrentService() {
		return currentService;
	}

	public void onLoadService() {
		loadding.setVisibility(View.VISIBLE);
		ServerController.getInstance(getActivity()).reuquestListServices(
				userLogin.getData().getToken(), "0",
				new OnRequest<ResponseServices>() {

					@Override
					public void onSuccess(ResponseServices data) {
						loadding.setVisibility(View.GONE);
						if (getActivity() == null)
							return;

						if (data.getResponsecode() >= 0) {
							mList = data.getData().getItems();
							ServiceAdapter adapter = new ServiceAdapter(
									getActivity(), R.layout.item_service, mList);
							mGridView.setAdapter(adapter);
						}
					}

					@Override
					public void onError(int errorCode, String message) {

					}
				});
	}

}
